package com.dkm.dkmproject.action;


import com.dkm.dkmproject.domain.BusinessTb;
import com.dkm.dkmproject.domain.UserTb;
import com.dkm.dkmproject.domain.vo.Gps;
import com.dkm.dkmproject.service.IBusinessTbService;
import com.dkm.dkmproject.service.IUserTbService;
import com.dkm.dkmproject.utils.FileTools;
import com.dkm.dkmproject.utils.Message;
import com.dkm.dkmproject.utils.Tools;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 业务管理 前端控制器
 * </p>
 *
 * @author 周佳佳
 * @since 2020-04-26
 */
@RestController
@RequestMapping("/dkm.dkmproject/businessTb")
@Api(description="业务管理接口文档")
public class BusinessTbController {

    @Autowired
    IBusinessTbService businessTbService;

    @Resource
    FileTools fileTools;
    
    @Autowired
    IUserTbService userTbService;

    @Resource
    Tools tools;
    /**
     * 增加业务管理的接口
     * @param businessTb
     * @return
     */
    @ApiOperation(value = " 增加业务管理的接口", notes = "成功则成功，失败返回失败")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "buId", value = "主键"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "userId", value = "业务人员id"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orId", value = "订单主键",required = true),
            @ApiImplicitParam(paramType = "query", dataType = "BigDecimal", name = "orMoney", value = "业务提成"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "exp1", value = "申请时间"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "exp2", value = "拓展字段2"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "exp3", value = "拓展字段3"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orContent", value = "用户问题描述",required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orImg", value = "用户照片描述"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orPhone", value = "用户联系电话",required = true),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orNote", value = "备注"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orFwname", value = "服务人名字"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orFwphone", value = "服务人电话")

    })
    @ApiResponses({
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "服务器拒绝请求"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 500, message = "后台报错"),
        @ApiResponse(code = 200, message = "返回成功")
    })
    @PostMapping("/addBusiness")
    public Message addBusiness(@RequestBody BusinessTb businessTb){
        Message message=new Message();
        if(businessTb.getFile1()!=null&&!"".equals(businessTb.getFile1())){
            String path=fileTools.saveFile("businessTb",businessTb.getFile1());
            businessTb.setOrImg(path);
        }
        List<UserTb> list=userTbService.selectUserId(businessTb.getUserId());
        if(list.size()!=0 &&list!=null){
            for (UserTb userTb1 : list) {
                businessTb.setOrFwname(userTb1.getUserName());
                businessTb.setOrFwphone(userTb1.getUserPhone());
                businessTb.setUserId(userTb1.getUserId());
                if(userTb1.getUserName()==""&&userTb1.getUserName().equals("")){
                    businessTb.setOrMoney("没有名字");
                }else{
                    businessTb.setOrMoney(userTb1.getUserName());
                }
            }
        }
      /*  GetLatAndLngByBaidu getLatAndLngByBaidu = new GetLatAndLngByBaidu();
        Object[] o =  getLatAndLngByBaidu.getCoordinate(businessTb.getExp3());
        System.out.println(businessTb.getExp3()+"======经度"+o[0].toString());
        System.out.println("======纬度"+o[1].toString());
        businessTb.setLongitude(o[0].toString());//经度
        businessTb.setLatitude(o[1].toString());//纬度*/
        int rows=businessTbService.insertSelective(businessTb);
        if(rows>0){
            message.setNum(1);
            message.setMsg("操作成功!");
        }else{
            message.setNum(0);
            message.setMsg("操作失败!");
        }
        return message;
    }
    /**
     * 根据订单id查询用户业务申请
     * @param orId
     * @return
     */
    @ApiOperation(value = " 根据订单id查询用户业务申请", notes = "成功则数据，失败返回空")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "buId", value = "主键"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "userId", value = "业务人员id"),
            @ApiImplicitParam(paramType = "query", dataType = "Integer", name = "orId", value = "订单主键",required = true),
            @ApiImplicitParam(paramType = "query", dataType = "BigDecimal", name = "orMoney", value = "业务提成"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "exp1", value = "申请时间"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "exp2", value = "拓展字段2"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "exp3", value = "拓展字段3"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orContent", value = "用户问题描述"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orImg", value = "用户照片描述"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orPhone", value = "用户联系电话"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orNote", value = "备注"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orFwname", value = "服务人名字"),
            @ApiImplicitParam(paramType = "query", dataType = "String", name = "orFwphone", value = "服务人电话")

    })
    @ApiResponses({
            @ApiResponse(code = 401, message = "没有权限"),
            @ApiResponse(code = 403, message = "服务器拒绝请求"),
            @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对"),
            @ApiResponse(code = 500, message = "后台报错"),
            @ApiResponse(code = 200, message = "返回成功")
    })
    @GetMapping("/findById")
    public List<BusinessTb> findById(String orId){
        List<BusinessTb> listOne=new ArrayList<>();
        List<BusinessTb> list=businessTbService.selectByPrimaryKey(orId);
        if(list.size()!=0 &&list!=null){
            for (BusinessTb businessTb : list) {
                businessTb.setDataOne(tools.getTimeStr(businessTb.getExp1()));
                listOne.add(businessTb);
            }
            return listOne;
        }else{
            return null;
        }
    }
    @RequestMapping("selectAll")
    @ResponseBody
    public Map<String,Object> selectAll(@RequestParam("page") Integer pageNum , @RequestParam("limit") Integer pageSize){
        Map<String,Object> map=new HashMap<>();
        map.put("count",businessTbService.selectCountTwo());

        map.put("code",0);
        map.put("msg","");
        map.put("data",businessTbService.selectAll(pageNum,pageSize));
        return map;
    }
    @RequestMapping("selectAllTwo")
    @ResponseBody
    public List<BusinessTb> selectAllTwo() {
        List<BusinessTb> listTwo=new ArrayList<>();
        //1 代表没人接单的
        List<BusinessTb> list = businessTbService.selectAllThree("1");
        if (list.size()!=0 &&list!=null) {
            for (BusinessTb businessTb : list) {
              /*  System.out.println("之前的经度========="+businessTb.getLongitude());
                System.out.println("之前的纬度========="+businessTb.getLatitude());*/
               /* Gps gcj = PositionUtil.
                        bd09_To_Gcj02(Double.valueOf(businessTb.getLatitude()),Double.valueOf(businessTb.getLongitude()));
                System.out.println("gcj :" + gcj.getLat());
                businessTb.setLatitude(String.valueOf(gcj.getLat()));
                businessTb.setLongitude(String.valueOf(gcj.getLon()));*/
                businessTb.setDataOne(tools.getTimeStr(businessTb.getExp1()));
             /*   System.out.println("之后的经度========="+businessTb.getLongitude());
                System.out.println("之后的纬度========="+businessTb.getLatitude());*/
                listTwo.add(businessTb);
            }
         }
        List<BusinessTb> listThree = businessTbService.selectAllThree("2");
        if (listThree.size()!=0 &&listThree!=null) {
            for (BusinessTb businessTb : listThree) {
                businessTb.setDataOne(tools.getTimeStr(businessTb.getExp1()));
                listTwo.add(businessTb);
            }
        }
        return listTwo;
    }
    @RequestMapping("updateIsva")
    @ResponseBody
    public Message updateIsva(BusinessTb businessTb){
        Message message=new Message();
        int rows=businessTbService.updateIsva(businessTb);
        if(rows>0){
            message.setNum(1);
            message.setMsg("操作成功");
        }else{
            message.setNum(0);
            message.setMsg("操作失败");
        }
        return message;
    }
    @RequestMapping("uploadFile")
    @ResponseBody
    public String uploadFile(@RequestParam("file1") MultipartFile file1){
        if(file1.getOriginalFilename()!=null &&!"".equals(file1.getOriginalFilename())){
            String path=fileTools.saveFiletwo("business",file1);
            return path;
        }else{
            return "没有上传文件";
        }
    }
    @RequestMapping("findByUserId")
    @ResponseBody
    public List<BusinessTb> findByUserId(){
        List<BusinessTb> businessTbList=new ArrayList<>();
        List<BusinessTb> list=businessTbService.findByUserId();
        for (BusinessTb businessTb : list) {
            String realname = businessTb.getWechatName();
            char[] r = realname.toCharArray();
            String realname1 =null;
            if(r.length ==1){ realname1 = realname; }
            if(r.length == 2){ realname1 = realname.replaceFirst(realname.substring(1),"*"); }
            if (r.length > 2) { realname1 = realname.replaceFirst(realname.substring(1,r.length-1) ,"*"); }
            businessTb.setWechatName(realname1);
            businessTb.setDataOne(tools.getTimeStr(businessTb.getExp1()));
            businessTbList.add(businessTb);
        }
        return businessTbList;
    }
}
